package com.yc.project3_bestMatching;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 任务类
 */
public class BestMatchingTask implements Callable<BestMatchingData> {
    private List<String> words; //最短距离的单词列表
    private String word;
    private int startIndex;
    private int endIndex;

    public BestMatchingTask(List<String> words, String word, int startIndex, int endIndex) {
        this.words = words;
        this.word = word;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    @Override
    public BestMatchingData call() throws Exception {
        List<String> minDistanceWords = new ArrayList<>(); //最短距离的单词列表
        int minDistance = Integer.MAX_VALUE;
        int distance ;//计算出的距离
        //循环words集合从 startIndex到endIndex,计算 word与这些单词之间的编辑距离
        for (int i= startIndex;i<endIndex;i++) {
            //记录最小的编辑距离及它对应的单词列表，
            distance = Editdistances.calculator(word, words.get(i));
            if (distance < minDistance) {
                minDistanceWords.clear(); //清空原有的
                minDistance= distance; ////记录新的最短距离
                minDistanceWords.add(words.get(i)); ////存这个新的最短距离对应的单词列表，
            } else if (distance == minDistance) { //如果这个单词与原来的最短距离单词一样距离，直接存.
                minDistanceWords.add(words.get(i));
            }
        }
        Thread.sleep(2000);
        ////将最小的编辑距离及单词列表包装成  BestMatchingData对象返回
        BestMatchingData result=new BestMatchingData();
        result.setDistance( minDistance);
        result.setWords( minDistanceWords) ;
        return result;
    }
}
